﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ninject;
using Meraz.Core.Managers;
using Meraz.Core.Model;
using Meraz.Web.Models.Facebook;
using Meraz.Web.Models;
using Meraz.Web.Filters;
using System.Net;
namespace Meraz.Web.Controllers
{
    public class FacebookController : BaseController
    {
        [Inject]
        public IFacebookManager FacebookManager { get; set; }
        public ActionResult Board(string id)
        {
            ViewData["Menu"] = Menu.Facebook;
            FacebookBoardModel model = new FacebookBoardModel();
            model.Board = FacebookManager.GetBoard(long.Parse(id));
            model.Sources = FacebookManager.GetSources();
            return View(model);
        }
        public ActionResult Boards()
        {
            ViewData["Menu"] = Menu.Facebook;
            FacebookBoardsModel model = new FacebookBoardsModel();
            model.Boards = FacebookManager.GetBoards();
            return View(model);
        }
        public ActionResult Index()
        {
            ViewData["Menu"] = Menu.Facebook;
            return View();
        }
        public ActionResult Post(string boardid, string topicid, string postid)
        {
            ViewData["Menu"] = Menu.Facebook;
            return Json(FacebookManager.GetPost(
                long.Parse(boardid),
                int.Parse(topicid),
                int.Parse(postid))
                );
        }
        public ActionResult Topic(string boardid, string topicid)
        {
            try
            {
                long boardID = long.Parse(boardid);
                int topicID = int.Parse(topicid);
                FacebookTopicModel model = new FacebookTopicModel();
                model.Topic = FacebookManager.GetTopic(boardID, topicID);
                string spark = (model.Topic.Posts.Count == 0) ? "Facebook/_BadTopic.spark" : "Facebook/_Topic.spark";

                string html = RenderViewToHtml<FacebookTopicModel>(spark, model);
                return Content(html);
            }
            catch (ApplicationException problem)
            {
                string message = problem.Message;
                var response = HttpContext.Response;
                response.StatusCode = (int)HttpStatusCode.InternalServerError;
                response.AddHeader("MerazHeader", message);
                response.Write(message);
                response.End();
                return null;
            }
            catch (Exception ex)
            {
                string message = "There was a problem retrieving this record: " + ex.Message;
                var response = HttpContext.Response;
                response.StatusCode = (int)HttpStatusCode.InternalServerError;
                response.AddHeader("MerazHeader", message);
                response.Write(message);
                response.End();
                return null;
            }
        }
        [ObjectFilter(Param = "topic", RootType = typeof(FacebookTopic))]
        [HttpPost]
        public ActionResult SaveTopic(FacebookTopic topic)
        {
            AjaxModel result = new AjaxModel();
            result.Status = AjaxStatus.Failure;
            try
            {
                FacebookManager.Save(topic);
                result.Status = AjaxStatus.Success;
            }
            catch (Exception ex)
            {
                result.Status = AjaxStatus.Error;
                result.Message = ex.ToString();
            }
            return Json(result);
        }

        [RolesAuthorize(Roles = UserRoles.Editor)]
        [ObjectFilter(Param = "post", RootType = typeof(FacebookTopicPost))]
        [HttpPost]
        public ActionResult SavePostLinks(FacebookTopicPost post)
        {
            if (!HttpContext.User.Identity.IsAuthenticated ||
            !System.Web.Security.Roles.IsUserInRole(UserRoles.Editor.ToString()))
                return null;


            AjaxModel result = new AjaxModel();
            result.Status = AjaxStatus.Failure;
            try
            {
                FacebookManager.SavePostLinks(post);
                result.Status = AjaxStatus.Success;
            }
            catch (ApplicationException problem)
            {
                string message = problem.Message;
                var response = HttpContext.Response;
                response.StatusCode = (int)HttpStatusCode.InternalServerError;
                response.AddHeader("MerazHeader", message);
                response.Write(message);
                response.End();
                return null;
            }
            catch (Exception ex)
            {
                string message = "There was a problem retrieving this record: " + ex.Message;
                var response = HttpContext.Response;
                response.StatusCode = (int)HttpStatusCode.InternalServerError;
                response.AddHeader("MerazHeader", message);
                response.Write(message);
                response.End();
                return null;
            }

            FacebookTopic topic = FacebookManager.GetTopic(post.BoardID, post.TopicID);
            RowModel model = new RowModel();
            result.Values.Add(topic.IDStr);
            result.Values.Add(topic.GetLink());
            result.Values.Add(topic.Poster);
            result.Values.Add(topic.CreatedDateString);
            result.Values.Add(topic.TotalString);
            result.Values.Add(topic.UniqueUsersString);
            return Json(result);
        }
        [ObjectFilter(Param = "topic", RootType = typeof(FacebookTopic))]
        [HttpPost]
        public ActionResult LoadTopic(FacebookTopic topic)
        {
            FacebookManager.LoadTopic(topic);
            RowModel model = new RowModel();
            model.Message = "Success";
            model.Values.Add(topic.IDStr);
            model.Values.Add(topic.GetLink());
            model.Values.Add(topic.Poster);
            model.Values.Add(topic.CreatedDateString);
            model.Values.Add(topic.TotalString);
            model.Values.Add(topic.UniqueUsersString);
            return Json(model);
        }
    }
}
